package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunSleepWifiMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcCsunSleepWifi;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.csunsleep.CsunSleepWifiDto;
import com.csun.cmny.provider.service.DmcCsunSleepWifiService;
import com.csun.cmny.provider.service.DmcDeviceService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Slf4j
@Transactional
public class DmcCsunSleepWifiServiceImpl extends BaseService<DmcCsunSleepWifi> implements DmcCsunSleepWifiService {

    @Resource
    private DmcCsunSleepWifiMapper dmcCsunSleepWifiMapper;

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Override
    public int setCsunSleepWifi(CsunSleepWifiDto csunSleepWifiDto, LoginAuthDto loginAuthDto) {

        DmcDevice device = dmcDeviceService.selectByKey(csunSleepWifiDto.getDeviceId());

        if(device == null){
            throw new DmcBizException(ErrorCodeEnum.DMC100610002,csunSleepWifiDto.getDeviceId());
        }
        if(!device.getDeviceType().equals(9)){
            throw new DmcBizException(ErrorCodeEnum.DMC100610033,csunSleepWifiDto.getDeviceId(),device.getDeviceType());
        }

        //查询是否有该条wifi设置数据
        DmcCsunSleepWifi dmcCsunSleepWifi = selectByKey(csunSleepWifiDto.getDeviceId());

        if(dmcCsunSleepWifi == null){
            dmcCsunSleepWifi = new DmcCsunSleepWifi();
            BeanUtils.copyProperties(csunSleepWifiDto,dmcCsunSleepWifi);
            dmcCsunSleepWifi.setId(csunSleepWifiDto.getDeviceId());
            dmcCsunSleepWifi.setUpdateInfo(loginAuthDto);
            return save(dmcCsunSleepWifi);
        }else{
            BeanUtils.copyProperties(csunSleepWifiDto,dmcCsunSleepWifi);
            dmcCsunSleepWifi.setId(csunSleepWifiDto.getDeviceId());
            dmcCsunSleepWifi.setUpdateInfo(loginAuthDto);
            return update(dmcCsunSleepWifi);
        }
    }
}
